package com.tensquare.sms.listener;

import com.aliyuncs.exceptions.ClientException;
import com.tensquare.sms.utils.SmsUtil;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Description：短信监听处理
 * @Author: liulei
 * @Date: 2019年10月31日
 * @Version: V1.0
 **/
@Component
@RabbitListener(queues = "sms")
public class SmsListener {

    @Autowired
    private SmsUtil smsUtil;

    @Value("${aliyun.sms.template_code}")
    private String templateCode;//模板号
    @Value("${aliyun.sms.sing_name}")
    private String singName;//签名

    @RabbitHandler
    public void executeSms(Map<String, String> map) {
        String mobile = map.get("mobile");//手机号码
        String checkcode = map.get("checkcode");//验证码

        System.out.println("手机号码：" +mobile);
        System.out.println("短信验证码：" +checkcode);
        try {
            smsUtil.sendSms(mobile, templateCode, singName, "{\"code\":\""+checkcode+"\"}");
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
